local joy__jianjie = fk.CreateSkill {

  name = "joy__jianjie",

  tags = {  },

}



joy__jianjie:addEffect("active", {
  name = "joy__jianjie",
  anim_type = "control",
  mute = true,
  can_use = function(self, player)
    return player:getMark("joy__jianjie-turn") == 0 and player:usedSkillTimes(joy__jianjie.name, Player.HistoryPhase) == 0
  end,
  interaction = function(self,player)
    return UI.ComboBox {choices = {"joy__dragon_mark_move", "joy__phoenix_mark_move"}}
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 2,
  target_filter = function(self, player, to_select, selected)
    if #selected == 2 or not self.interaction.data then return false end
    local to = Fk:currentRoom():getPlayerById(to_select)
    local mark = (self.interaction.data == "joy__dragon_mark_move") and "@@joy__dragon_mark" or "@@joy__phoenix_mark"
    if #selected == 0 then
      return to:getMark(mark) > 0
    else
      return to:getMark(mark) == 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:notifySkillInvoked(player, joy__jianjie.name)
    local from = effect.tos[1]
    local to = room:getPlayerById(effect.tos[2])
    local mark = (self.interaction.data == "joy__dragon_mark_move") and "@@joy__dragon_mark" or "@@joy__phoenix_mark"
    doJianjieMarkChange (room, from, mark, false, player)
    doJianjieMarkChange (room, to, mark, true, player)
  end,
})

joy__jianjie:addEffect(fk.TurnStart, {
  name = "#joy__jianjie_trigger",
  events = {fk.TurnStart, fk.Death},
  --joy__jianjie,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(joy__jianjie.name) and player:getMark("joy__jianjie-turn") > 0
    else
      return player:hasSkill(joy__jianjie.name) and (target:getMark("@@joy__dragon_mark") > 0 or target:getMark("@@joy__phoenix_mark") > 0)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnStart then return true end
    local room = player.room
    local gives = {}
    if target:getMark("@@joy__dragon_mark") > 0 then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__dragon_mark") == 0 end)
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__dragon_mark-move::"..target.id, joy__jianjie.name, true)
        if #tos > 0 then
          table.insert(gives, {"@@joy__dragon_mark", tos[1]})
        end
      end
    end
    if target:getMark("@@joy__phoenix_mark") > 0 then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__phoenix_mark") == 0 end)
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__phoenix_mark-move::"..target.id, joy__jianjie.name, true)
        if #tos > 0 then
          table.insert(gives, {"@@joy__phoenix_mark", tos[1]})
        end
      end
    end
    if #gives > 0 then
      event:setCostData(self, gives)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "joy__jianjie")
    if event == fk.TurnStart then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__dragon_mark") == 0 end)
      local dra
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__dragon_mark-give", joy__jianjie.name, false)
        if #tos > 0 then
          dra = room:getPlayerById(tos[1])
          doJianjieMarkChange (room, dra, "@@joy__dragon_mark", true, player)
        end
      end
      local pho_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__phoenix_mark") == 0 end)
      table.removeOne(pho_tars, dra)
      if #pho_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(pho_tars, Util.IdMapper), 1, 1, "#joy__phoenix_mark-give", joy__jianjie.name, false)
        if #tos > 0 then
          local pho = room:getPlayerById(tos[1])
          doJianjieMarkChange (room, pho, "@@joy__phoenix_mark", true, player)
        end
      end
    else
      for _, dat in ipairs(event:getCostData(self)) do
        local mark = dat[1]
        local p = room:getPlayerById(dat[2])
        doJianjieMarkChange (room, p, mark, true, player)
      end
    end
  end,
})
joy__jianjie:addEffect(fk.Death, {
  name = "#joy__jianjie_trigger",
  events = {fk.TurnStart, fk.Death},
  --joy__jianjie,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(joy__jianjie.name) and player:getMark("joy__jianjie-turn") > 0
    else
      return player:hasSkill(joy__jianjie.name) and (target:getMark("@@joy__dragon_mark") > 0 or target:getMark("@@joy__phoenix_mark") > 0)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnStart then return true end
    local room = player.room
    local gives = {}
    if target:getMark("@@joy__dragon_mark") > 0 then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__dragon_mark") == 0 end)
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__dragon_mark-move::"..target.id, joy__jianjie.name, true)
        if #tos > 0 then
          table.insert(gives, {"@@joy__dragon_mark", tos[1]})
        end
      end
    end
    if target:getMark("@@joy__phoenix_mark") > 0 then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__phoenix_mark") == 0 end)
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__phoenix_mark-move::"..target.id, joy__jianjie.name, true)
        if #tos > 0 then
          table.insert(gives, {"@@joy__phoenix_mark", tos[1]})
        end
      end
    end
    if #gives > 0 then
      event:setCostData(self, gives)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "joy__jianjie")
    if event == fk.TurnStart then
      local dra_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__dragon_mark") == 0 end)
      local dra
      if #dra_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(dra_tars, Util.IdMapper), 1, 1, "#joy__dragon_mark-give", joy__jianjie.name, false)
        if #tos > 0 then
          dra = room:getPlayerById(tos[1])
          doJianjieMarkChange (room, dra, "@@joy__dragon_mark", true, player)
        end
      end
      local pho_tars = table.filter(room.alive_players, function(p) return p:getMark("@@joy__phoenix_mark") == 0 end)
      table.removeOne(pho_tars, dra)
      if #pho_tars > 0 then
        local tos = room:askForChoosePlayers(player, table.map(pho_tars, Util.IdMapper), 1, 1, "#joy__phoenix_mark-give", joy__jianjie.name, false)
        if #tos > 0 then
          local pho = room:getPlayerById(tos[1])
          doJianjieMarkChange (room, pho, "@@joy__phoenix_mark", true, player)
        end
      end
    else
      for _, dat in ipairs(event:getCostData(self)) do
        local mark = dat[1]
        local p = room:getPlayerById(dat[2])
        doJianjieMarkChange (room, p, mark, true, player)
      end
    end
  end,
})
joy__jianjie:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart, fk.EventAcquireSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(self,true) and target == player
    else
      return target == player and data == self and player.room:getTag("RoundCount")
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local current_event = room.logic:getCurrentEvent()
    if not current_event then return end
    local turn_event = current_event:findParent(GameEvent.Turn, true)
    if not turn_event then return end
    local events = room.logic.event_recorder[GameEvent.Turn] or Util.DummyTable
    for _, e in ipairs(events) do
      local current_player = e.data.who
      if current_player == player then
        if turn_event.id == e.id then
          room:setPlayerMark(player, "joy__jianjie-turn", 1)
        end
        break
      end
    end
  end,
})
joy__jianjie:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.TurnStart, fk.EventAcquireSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(self,true) and target == player
    else
      return target == player and data == self and player.room:getTag("RoundCount")
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local current_event = room.logic:getCurrentEvent()
    if not current_event then return end
    local turn_event = current_event:findParent(GameEvent.Turn, true)
    if not turn_event then return end
    local events = room.logic.event_recorder[GameEvent.Turn] or Util.DummyTable
    for _, e in ipairs(events) do
      local current_player = e.data
      if current_player == player then
        if turn_event.id == e.id then
          room:setPlayerMark(player, "joy__jianjie-turn", 1)
        end
        break
      end
    end
  end,
})

return joy__jianjie